package com.tsky.common;

import java.lang.reflect.Method;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

/**
 * 工具类：JAVA BEAN操作
 * @author: cainiao
 * @version 1.0 
 * @time: 2010-6-13 上午07:01:52
 */
public final class BeanUtil {

	private static long idValue = System.currentTimeMillis();
	private static SimpleDateFormat sdf = new SimpleDateFormat(SysUtil.LONG_FORMAT);

	/**
	 * 获取18的主键
	 * @author: cainiao
	 * @time: 2010-6-13 上午06:21:51
	 * @function: 
	 * @return
	 */
	private static synchronized long get18BitID() {
		return idValue++;
	}

	/**
	 * 
	 * @author：cainiao
	 * @方法描述：获取18位的ID
	 * @date: 2011-3-16
	 * @return
	 */
	public static String getID() {
		return sdf.format(new Date(get18BitID()));
	}
	
	/**
	 * 
	 * @author：cainiao
	 * @方法描述：MD5加密，来自Juyon
	 * @date: 2011-3-16
	 * @param source
	 * @return
	 */
	public static String getMd5(String source) {
		return MD5Util.getMD5(source);
	}
	
	/**
	 * 获取已经设置ID的实体类实例
	 * @author: cainiao
	 * @time: 2010-6-13 上午06:22:27
	 * @function: 
	 * @param entityClass
	 * @return
	 */
	public static Object getInstance(Class<?> entityClass){
		try {
			Object entity = entityClass.newInstance();
			Method method = entityClass.getDeclaredMethod("setId", new Class[] {String.class });
			String id = BeanUtil.getID();
			method.invoke(entity, new Object[] {id });
			return entity;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			SysUtil.printStackTrace(e);
		} 
		return null;
	}
	
	/**
	 * 获取已经设置ID的实体类实例
	 * @author: cainiao
	 * @time: 2010-6-13 上午06:23:38
	 * @function: 
	 * @param entityClass
	 * @param idSetMethod
	 * @return
	 */
	public static Object getInstance(Class<?> entityClass, String idSetMethod){
		try {
			Object entity = entityClass.newInstance();
			Method method = entityClass.getDeclaredMethod(idSetMethod, new Class[] {String.class });
			String id = BeanUtil.getID();
			method.invoke(entity, new Object[] {id });
			return entity;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			SysUtil.printStackTrace(e);
		} 
		return null;
	}
	
	/**
	 * 实例的属性拷贝
	 * @author: cainiao
	 * @time: 2010-6-13 上午06:24:11
	 * @function: 
	 * @param target
	 * @param source
	 */
	public static void copyProperties(Object target, Object source) {
		Class<?> targetCT = target.getClass();
		Class<?> sourceCT = source.getClass();
		
		Method[] targetMethods = targetCT.getMethods();
		
		String methodName = null;
		String getMethod = null;
		Object getVal = null;
		Object[] paramsValue = null;
		
		for (Method method : targetMethods) {
			methodName = method.getName();
			
			if("set".equals(methodName.substring(0, 3))){
				getMethod = "get" + methodName.substring(3);
				try {
					getVal = sourceCT.getMethod(getMethod).invoke(source);
					// 存在空值不进行copy
					if (!isEmpty(getVal)){
						paramsValue = new Object[] {getVal };
						method.invoke(target, paramsValue);
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					// SysUtil.printStackTrace(e);
				} 
			}
		}
	}
	
	/**
	 * 调用实例的方法
	 * @author: cainiao
	 * @time: 2010-6-13 上午06:24:29
	 * @function: 
	 * @param entity
	 * @param methodName
	 * @return
	 */
	public static Object invoke(Object entity, String methodName) {
		Class<?> classType = entity.getClass();

		try {
			return classType.getMethod(methodName).invoke(entity);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			SysUtil.printStackTrace(e);
		} 

		return null;
	}
	
	/**
	 * 数据是否为空
	 * @param value
	 * @return
	 */
	public static boolean isEmpty(Object value){
		return (value == null || value.equals("") || value.toString().trim().equals(""));
	}
	
	public static boolean isEmpty(Object[] array){
		return (array == null || array.length == 0);
	}
	
	public static boolean isEmpty(List<?> list){
		return list == null? true: list.isEmpty();
	}
	
	/**
	 * 格式化为时刻
	 * @author: cainiao
	 * @time: 2010-4-21 下午03:08:07
	 * @function: 
	 * @param timeStr
	 * @return
	 */
	public static Timestamp formatToTime(String timeStr) {
		Date time = new Date();
		if(!isEmpty(timeStr)){
			try {
				time = new SimpleDateFormat(SysUtil.TIME_FORMAT).parse(timeStr);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return new Timestamp(time.getTime());
	}
	
	public static Timestamp formatToTime_HHMM(String timeStr) {
		Date time = new Date();
		if(!isEmpty(timeStr)){
			try {
				time = new SimpleDateFormat(SysUtil.TIME_FORMAT_HHMM).parse(timeStr);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return new Timestamp(time.getTime());
	}
	
	/**
	 * 格式化为时刻
	 * @author: cainiao
	 * @time: 2010-4-21 下午03:08:07
	 * @function: 
	 * @param time
	 * @return
	 */
	public static String formatToTime(Timestamp time) {
		return time == null ? ""
				: new SimpleDateFormat(SysUtil.TIME_FORMAT).format(time);
	}
	
	/**
	 * 格式化为时刻
	 * @author: cainiao
	 * @time: 2010-4-21 下午03:08:07
	 * @function: 
	 * @param timeStr
	 * @return
	 */
	public static Timestamp formatToDate(String timeStr) {
		Date time = new Date();
		if(!isEmpty(timeStr)){
			try {
				time = new SimpleDateFormat(SysUtil.DATE_FORMAT).parse(timeStr);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return new Timestamp(time.getTime());
	}
	
	/**
	 * 格式化为日期
	 * @author: cainiao
	 * @time: 2010-4-21 下午02:56:29
	 * @function: 
	 * @param time
	 * @return
	 */
	public static String formatToDate(Timestamp time) {
		return time == null ? ""
				: new SimpleDateFormat(SysUtil.DATE_FORMAT).format(time);
	}
	
	/**
	 * 格式化为日期
	 * @author：cainiao
	 * @方法描述：
	 * @date: 2011-3-16
	 * @param time
	 * @return
	 */
	public static String formatToDate(Date time) {
		return time == null ? ""
				: new SimpleDateFormat(SysUtil.DATE_FORMAT).format(time);
	}
	
	/**
	 * 获取系统当前的时间
	 * @author: cainiao
	 * @time: 2010-6-13 上午06:31:57
	 * @function: 
	 * @return
	 */
	public static Timestamp getTimeStamp(){
		TimeZone.setDefault(TimeZone.getTimeZone(SysUtil.TIME_ZONE));
		return new Timestamp(System.currentTimeMillis());
	}
	
	/**
	 * 获取系统的当年年份
	 * @author: cainiao
	 * @time: 2010-6-13 上午06:30:28
	 * @function: 
	 * @return
	 */
	public static String getThisYear(){
		Timestamp thisTime = getTimeStamp();
		String thisYear = formatToDate(thisTime);
		return thisYear.substring(0, 4);
	}
	
	/**
	 * 根据总条数和页大小得出最后一夜
	 * @param total 总条数
	 * @param pageSize 页大小
	 * @return 最后页数
	 */
	public static int getLastPageCount(int total, int pageSize) {
		int lastPage = 1;
		if(total != 0) {
			if(total%pageSize == 0){
				lastPage = total/pageSize;
			} else {
				lastPage = total/pageSize + 1;
			}
		}
		return lastPage;
	}
	
}
